<?php

namespace app\command;

use app\common\library\Shaper;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;

class CreateShaper extends Command
{
    protected const ADD_TOKEN_NUM = 5;

    protected function configure()
    {
        $this->setName('shaper')
            ->addOption('type', mode: Option::VALUE_REQUIRED)
            ->setDescription('Increase the token in the bucket');
    }

    protected function execute(Input $input, Output $output): void
    {
        if (!$input->hasOption('type')) {
            $output->warning('Option is empty');
            return;
        }
        $type = $input->getOption('type');
        if (!in_array($type, array('add', 'reset'))) {
            $output->warning('Input shaper type err');
            return;
        }
        $model = new Shaper(container: 'container');
        if ($type == 'add') {
            $model->add(self::ADD_TOKEN_NUM);
        } elseif ($type == 'reset') {
            $model->resetBucketToken();
        }
        $output->info('Add shaper token success!');
    }
}